package com.wanda.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.DataModel;
import com.wanda.service.DataService;
import com.wanda.service.impl.DataServiceimpl;
import com.wanda.util.ExcelDataUtil;


@Controller
@RequestMapping("qt/data") 
public class DataController {
   
		@Resource
	    private DataService dataService;
		
	   public List<DataModel> selectSqlData(String SearchSql){
		   List<DataModel> mList = null;
		   try{
			   mList=  dataService.selectBySql(SearchSql);
		   }catch(Exception e ){
			   e.printStackTrace();
		   }finally{
			   
		   }
		   return mList;
		   
	   }
	   //导入
		@RequestMapping(value = "/upload.do")
		public @ResponseBody ModelAndView ImportMyroom(String path) {
			try {
				ModelAndView mav = new ModelAndView();
				JFileChooser jfc = new JFileChooser(path);
				int n = jfc.showOpenDialog(null);
				File file = jfc.getSelectedFile();
				if (file != null) {
					String[][] result = ExcelDataUtil.getData(file, 1);
					int rowLength = result.length;
					for (int i = 0; i < rowLength; i++) {
						// 将读取的数据存入到数据库
						DataModel data = new DataModel();
						data.setiD(result[i][1]);
						data.setTask_ID(result[i][2]);
						data.setData_URL(result[i][3]);
						data.setField_1(result[i][4]);
						data.setField_2(result[i][5]);
						data.setField_3(result[i][6]);
						data.setField_4(result[i][7]);
						data.setField_5(result[i][8]);
						data.setField_6(result[i][9]);
						data.setField_7(result[i][10]);
						data.setField_8(result[i][11]);
						data.setField_9(result[i][12]);
						data.setField_10(result[i][13]);
						data.setField_11(result[i][14]);
						data.setField_12(result[i][15]);
						data.setField_13(result[i][16]);
						data.setField_14(result[i][17]);
						data.setField_15(result[i][18]);
						data.setField_16(result[i][19]);
						data.setField_17(result[i][20]);
						data.setField_18(result[i][21]);
						data.setField_19(result[i][22]);
						data.setField_20(result[i][23]);
						data.setField_21(result[i][24]);
						data.setField_22(result[i][25]);
						data.setField_23(result[i][26]);
						data.setField_24(result[i][27]);
						data.setField_25(result[i][28]);
						data.setField_26(result[i][29]);
						data.setField_27(result[i][30]);
						data.setField_28(result[i][31]);
						data.setField_29(result[i][32]);
						data.setField_30(result[i][33]);
						data.setField_31(result[i][34]);
						data.setField_32(result[i][35]);
						data.setField_33(result[i][36]);
						data.setField_34(result[i][37]);
						data.setField_35(result[i][38]);
						data.setField_36(result[i][39]);
						data.setField_37(result[i][40]);
						data.setField_38(result[i][41]);
						data.setField_39(result[i][42]);
						data.setField_40(result[i][43]);
						data.setField_41(result[i][44]);
						data.setField_42(result[i][45]);
						data.setField_43(result[i][46]);
						data.setField_44(result[i][47]);
						data.setField_45(result[i][48]);
						data.setField_46(result[i][49]);
						data.setField_47(result[i][50]);
						data.setField_48(result[i][51]);
						data.setField_49(result[i][52]);
						data.setField_50(result[i][53]);
						data.setCreate_User(result[i][54]);
						data.setCreate_Time(result[i][55]);
						data.setUpdate_User(result[i][56]);         
						data.setUpdate_time(result[i][57]);

						dataService.addData(data);
					} 
					JOptionPane.showMessageDialog(null, "导入成功");
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			ModelAndView mav1 = new ModelAndView();
			mav1.setViewName("select.do");
	        return mav1;
		}
		//导出
		@RequestMapping(value = "/dataExport.do")
		public   @ResponseBody ModelAndView  export(String path) {
			ModelAndView mav = new ModelAndView();
			mav.setViewName("indent");
			try {
				DataService data = new DataServiceimpl();
				List<DataModel> indents = data.selectBySql("");
				List<String> leader = new ArrayList<String>();
				ExcelDataUtil.exportExcel_Code(leader, indents, "sheet1","d://data.xls");
				JOptionPane.showMessageDialog(null, "文件已导出在d盘");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			ModelAndView mav1 = new ModelAndView();
			mav1.setViewName("select.do");
        return mav;
		}    
	   //注释
}
